<template>
	<view class="">
		<view class="main">
			<button class="usertop" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" v-if="!is_login">
				<image :src="info.avatar_path || '/static/user.png'" mode="widthFix"></image>
				<view class="info">
					<view class="name">
						{{info.nickName || '点击授权登录'}}
					</view>
					<view class="phone">
						{{info.phone || '未登录'}}
					</view>
				</view>
			</button>
			<button class="usertop" v-else>
				<image :src="info.avatar_path || '/static/user.png'" mode="widthFix"></image>
				<view class="info">
					<view class="name">
						{{info.nickName || '点击授权登录'}}
					</view>
					<view class="phone">
						{{info.phone || '未登录'}}
					</view>
				</view>
			</button>
			<view class="list">

				<view class="item" @click="toAppoint">
					<view class="left">
						<uni-icons class="icon" type="bars" :size="20" color="#1989fa"></uni-icons>
						预约记录
					</view>
					<uni-icons type="right" color="#999"></uni-icons>
				</view>
				<view class="item" @click="loginPage('/pages/setting/index',is_login)">
					<view class="left">
						<uni-icons class="icon" type="staff" :size="20" color="#1989fa"></uni-icons>
						修改个人信息
					</view>
					<uni-icons type="right" color="#999"></uni-icons>
				</view>
				<!-- #ifdef MP-WEIXIN -->
				<view class="item" @click="scan" v-if="is_login && info.write_off">
					<view class="left">
						<uni-icons class="icon" type="scan" :size="20" color="#1989fa"></uni-icons>
						扫码核销
					</view>
					<uni-icons type="right" color="#999"></uni-icons>
				</view>
				<!-- #endif -->
				<view class="item" v-if="is_login" @click="outLogin">
					<view class="left">
						<uni-icons class="icon" type="cloud-download" :size="20" color="#f00"></uni-icons>
						退出登录
					</view>
					<uni-icons type="right" color="#999"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		userIndex,
		getMpOpenid,
		mpLogin
	} from "@/api/api.js"
	export default {
		data() {
			return {
				info: [],
				open_id: '',
				is_login: false
			}
		},
		onLoad() {
			this.getMpOpenid()
		},
		onShow() {
			this.userIndex()
		},
		methods: {
			scan() {
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success(res) {
						uni.$showMsg(res.scanType + res.result)
						if (res.result != '') {
							uni.navigateTo({
								url: `/pages/scan/index?order_id=${res.result}`
							})
						}
					}
				})
			},
			outLogin() {
				uni.removeStorageSync('token')
				this.is_login = false
				this.info = []
				uni.$showMsg('退出成功')
			},
			getMpOpenid() {
				var that = this
				// #ifdef MP-WEIXIN
				uni.login({
					success(res) {
						getMpOpenid({
							code: res.code
						}).then(data => {
							that.open_id = data.data.openid
						})
					}
				})
				// #endif
			},
			getPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					let encryptedData = e.detail.encryptedData
					let errMsg = e.detail.errMsg
					let iv = e.detail.iv
					let code = e.detail.code
					mpLogin({
						openid: this.open_id,
						code: code
					}).then(res => {
						if (res.status == 1) {
							let data = res.data
							uni.setStorageSync('token', data.token)
							this.info = {
								nickName: data.nickName,
								phone: data.phone,
								avatar_path: data.avatar_path
							}
							this.userIndex()
							this.is_login = true
						}
					})
				}
			},
			userIndex() {
				userIndex().then(res => {
					if (res.status == 1) {
						this.info = res.data
						this.is_login = true
					} else {

					}
				})
			},
			toAppoint() {
				uni.switchTab({
					url: '/pages/appoint/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f3f5f8;
	}

	.usertop {
		width: 96%;
		margin: 10px auto;
		background: #8E060fe0;
		border-radius: 5px;
		display: flex;
		align-items: center;
		padding-left: 8%;
		box-sizing: border-box;
		height: 100px;
		margin-top: 20px;
		line-height: unset;
		text-align: unset;

		image {
			width: 60px;
			border-radius: 5px;
		}

		.info {
			color: #fff;
			padding-left: 5%;
			font-size: 14px;

			.name {
				font-size: 15px;
				font-weight: bold;
				margin-bottom: 3px;
			}
		}

	}

	.list {
		padding: 20px 2%;

		.item {
			background: #fff;
			padding: 12px;
			font-size: 15px;
			display: flex;
			justify-content: space-between;
			margin-bottom: 15px;
			color: #999;

			.left {
				display: flex;
				align-items: center;
			}

			.icon {
				margin-right: 10px;
			}
		}
	}
</style>